package com.example.zhu95.mytimber.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.PlayListBean;
import com.example.zhu95.mytimber.utils.Util;

import java.util.List;

/**
 * @author zhu95
 * @date 2018/2/12 19:35
 */
public class MutilViewPageFragment extends Fragment {
    int position = 0;
    List<PlayListBean> playlists = null;
    View.OnClickListener itemClickListener;
    View view;

    public static MutilViewPageFragment create(int position, List<PlayListBean> playlists, View.OnClickListener listener) {
        MutilViewPageFragment fragment = new MutilViewPageFragment();
        fragment.position = position;
        fragment.playlists = playlists;
        fragment.itemClickListener = listener;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.multi_view_pager, container, false);

        View foregroundView = view.findViewById(R.id.foreground_view);
        int foreColor = Util.INSTANCE.getRandomForeColor();
        foregroundView.setBackgroundColor(foreColor);
        foregroundView.setTag(R.string.share_playlist_fore, foreColor);
        view.setOnClickListener(itemClickListener);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // playlists = PlaylistUtils.INSTANCE.getAllPlaylistData();

        TextView tvNum = view.findViewById(R.id.tv_num);
        TextView tvPlaylistName = view.findViewById(R.id.tv_playlist_name);
        TextView tvAutoPlaylist = view.findViewById(R.id.tv_auto_playlist);
        TextView tvPlaylistSongCount = view.findViewById(R.id.tv_playlist_song_count);
        ImageView imageMultiPager = view.findViewById(R.id.image_multi_pager);
        if (position < 9) {
            tvNum.setText("0" + (position + 1));
        } else {
            tvNum.setText("" + (position + 1));
        }
        final PlayListBean playList = playlists.get(position);
        if (playList.isDefaultList()) {
            tvAutoPlaylist.setVisibility(View.VISIBLE);
        } else {
            tvAutoPlaylist.setVisibility(View.GONE);
        }
        tvPlaylistName.setText(playList.getTitle());
        List<String> songIDs = playList.getSongIDs();
        int songCount = 0;
        if (songIDs != null) {
            songCount = songIDs.size();
        }
        tvPlaylistSongCount.setText("" + songCount + " 歌曲");

        String albumArt = playList.getAlbumArt();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(view.getContext()).load(albumArt).centerCrop().into(imageMultiPager);
        } else {
            Glide.with(view.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(imageMultiPager);
        }

        view.setTag(playList.getTitle());
    }
}
